/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package LinkImageBN;

import DataAccess.FindResult;
import DataAccess.LinkImageDA;
import HouseBN.HouseService;
import entity.House;
import entity.LinkImage;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Mr-Quick
 */
public class LinkimageService {

    EntityManager em;

    public LinkimageService() {
        String persistenceUnitName = "SweetHomePU";
        EntityManagerFactory factory = Persistence.createEntityManagerFactory(persistenceUnitName);
        em = factory.createEntityManager();
    }

    public List<LinkImage> Getlinkimagebyidhouse(String idhouse) {
        HouseService hs = new HouseService();
        House h = hs.Gethousebyid(idhouse);
        LinkImageDA LIDA = new LinkImageDA(em);
        try {
            FindResult<LinkImage> li = LIDA.findAbsolutely("idImage", h);
            return li;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public LinkImage Getlinkimagebyid(int idphoto) {
        LinkImageDA lida = new LinkImageDA(em);
        FindResult<LinkImage> li = lida.findAbsolutely("id", idphoto);
        if (li.isEmpty()) {
            return null;
        } else {
            return li.get(0);
        }
    }

    public void deletelinkImageHouse(int idphoto) {
        try {
            LinkImageDA lida = new LinkImageDA(em);
            LinkImage li = Getlinkimagebyid(idphoto);
            em.getTransaction().begin();
            lida.remove(li);
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void deleteimagebyiduser(List<LinkImage> listimg) {
        try {
            LinkImageDA lida = new LinkImageDA(em);
            em.getTransaction().begin();
            for (int i = 0; i < listimg.size(); i++) {
                lida.remove(listimg.get(i));
            }
            em.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}
